package persistent.impl;

import java.util.ArrayList;
import java.util.List;

import basic.HibBaseDAO;
import model.TBackRole;
import model.TBackRoleSystemModel;
import model.VBackRoleSystemModel;
import persistent.dao.BackRoleDAO;

/**
 * 管理员角色业务类接口的实现
 * @author djf
 * @since 2018/11/21
 */
public class BackRoleImpl implements BackRoleDAO {

	private HibBaseDAO dao;
	
	public BackRoleImpl() {
		dao=new HibBaseDAO();
	}

	/**
	 * 添加一个管理员角色
	 * @param backrole 管理员角色对象
	 * @return 成功返回true,失败返回false
	 */
	@Override
	public boolean addBackRole(TBackRole backrole) {
		String procname="up_Back_addRole(?,?,?)";
		Object para[]={backrole.getName(),backrole.getDescription(),backrole.getParentid()};
		int row=(Integer)dao.executeProduce(procname, para);
		if(row>0){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 修改一个管理员角色类型
	 * @param backrole 管理员角色对象
	 * @return 成功返回true,失败返回false
	 */
	@Override
	public boolean updateRole(TBackRole backrole) {
		TBackRole backrole2=(TBackRole)dao.findById(TBackRole.class, backrole.getId());
		backrole2.setName(backrole.getName());
		backrole2.setDescription(backrole.getDescription());
		backrole2.setDeepth(backrole.getDeepth());
		backrole2.setParentid(backrole.getParentid());
		backrole2.setStatus(backrole.getStatus());
		return dao.update(backrole2);
	}

	/**
	 * 得到一个管理员角色对象
	 * @param backroleid 管理员角色id
 	 * @return 成功返回TBackRole对象，失败返回null
	 */
	@Override
	public TBackRole getTBackRole(int backroleid) {
		return (TBackRole)dao.findById(TBackRole.class, backroleid);
	}

	/**
	 * 通过查询条件得到管理员角色列表
	 * @param strwhere 查询条件
	 * @return 成功返回TBackRole列表，失败返回null
	 */
	@Override
	public List<TBackRole> getBackRoleList(String strwhere) {
		String hql="from TBackRole "+strwhere;
		return dao.select(hql);
	}

	/**
	 * 通过查询条件得到管理员角色列表,带分页
	 * @param strwhere 查询条件
	 * @param currentPage 当前页
	 * @param pageSize 每页数
	 * @return 成功返回TBackRole列表，失败返回null
	 */
	@Override
	public List<TBackRole> getBackRoleList(String strwhere, int currentPage,
			int pageSize) {
		String hql="from TBackRole "+strwhere;
		return dao.selectByPage(hql, currentPage, pageSize);
	}

	/**
	 * 根据角色查询菜单列表(不分页)
	 * @param strwhere 查询条件
	 * @return 成功返回VBackRoleSystemModel列表，失败返回null
	 */
	@Override
	public List<VBackRoleSystemModel> getBackRoleSystemModelMenu(String strwhere) {
		String hql="from VBackRoleSystemModel "+strwhere;
		return dao.select(hql);
	}

	
	/**
	 * 根据角色查询出角色权限(不分页)
	 * @param strwhere 查询条件
	 * @return 成功返回VBackRoleSystemModel列表，失败返回null
	 */
	@Override
	public List<VBackRoleSystemModel> getBackRoleSystemModel(String strwhere) {
		
		List<VBackRoleSystemModel> rslist = new ArrayList<VBackRoleSystemModel>();
		String hql="from VBackRoleSystemModel "+strwhere;
		List<VBackRoleSystemModel> list = dao.select(hql);//先得到展开菜单信息
		for(int i=0;i<list.size();i++){
			VBackRoleSystemModel obj = (VBackRoleSystemModel)list.get(i);
			rslist.add(obj); //先将 展开菜单对象加入 
			String hql2 = "from VBackRoleSystemModel where roleid = "+ obj.getRoleid() 
					+" and menuParentid = "+ obj.getSysid() +" order by displayorder";
			List<VBackRoleSystemModel> list2 = dao.select(hql2);
			for(int j=0;j<list2.size();j++){
				rslist.add(list2.get(j));
			}
		}
		return rslist;
	}
	
	/**
	 * 根据角色查询出角色权限(分页)
	 * @param strwhere 查询条件
	 * @param currentPage 当前页
	 * @param pageSize 每页数
	 * @return 成功返回VBackRoleSystemModel列表，失败返回null
	 */
	@Override
	public List<VBackRoleSystemModel> getBackRoleSystemModel(String strwhere,
			int currentPage, int pageSize) {
		String hql="from VBackRoleSystemModel "+strwhere;
		return dao.selectByPage(hql, currentPage, pageSize);
	}

	/**
	 * 通过角色权限id得到一个角色权限对象
	 * @param id
	 * @return
	 */
	@Override
	public TBackRoleSystemModel getTBackRoleSystemModel(int id) {
		return (TBackRoleSystemModel)dao.findById(TBackRoleSystemModel.class, id);
	}
	
	/**
	 * 修改角色权限(授予、取消)
	 * @param backrolemodel 角色权限
	 * @return 成功返回true，失败返回false
	 */
	@Override
	public boolean updateBackRoleSystemModel(TBackRoleSystemModel backrolemodel) {
		TBackRoleSystemModel rolemodel2=(TBackRoleSystemModel)dao.findById(TBackRoleSystemModel.class, 
				backrolemodel.getId());
		rolemodel2.setRoleid(backrolemodel.getRoleid());
		rolemodel2.setSysid(backrolemodel.getSysid());
		rolemodel2.setIsedit(backrolemodel.getIsedit());
		rolemodel2.setCreatedate(backrolemodel.getCreatedate());
		return dao.update(rolemodel2);
	}

	/**
	 * 启用一个权限
	 * @param powerid 权限编号
	 * @return 成功返回true，失败返回false
	 */
	@Override
	public boolean enable(int powerid) {
		VBackRoleSystemModel rolesysmodel = (VBackRoleSystemModel)dao.findById(VBackRoleSystemModel.class, powerid);
		TBackRoleSystemModel tSystemModel = (TBackRoleSystemModel) dao.findById(TBackRoleSystemModel.class, powerid);
		if (rolesysmodel.getMenuDeepth() != 0) {
			String hql = "from VBackRoleSystemModel where sysid = " + rolesysmodel.getMenuParentid() 
					+ " and roleid = " + rolesysmodel.getRoleid();
			List<VBackRoleSystemModel> roleList = dao.select(hql);
			if (roleList != null && roleList.size() > 0) {
				if (roleList.get(0).getIsedit()) {
					tSystemModel.setIsedit(true);
					return dao.update(tSystemModel);
				}
				else {
					return false;
				}	
			}
			else {
				return false;
			}
				
		}
		else {
			tSystemModel.setIsedit(true);
			return dao.update(tSystemModel);
		}		
	}
	
	/**
	 * 停用一个权限
	 * @param powerid 权限编号
	 * @return 成功返回true，失败返回false
	 */
	@Override
	public boolean disEnable(int powerid) {
		VBackRoleSystemModel rolesysmodel=(VBackRoleSystemModel)
				dao.findById(VBackRoleSystemModel.class, powerid);
		
		if (rolesysmodel.getMenuDeepth() != 0) {	
			TBackRoleSystemModel trolesysmodel = (TBackRoleSystemModel) 
					dao.findById(TBackRoleSystemModel.class, powerid);
			trolesysmodel.setIsedit(false);
			return dao.update(trolesysmodel);				
		}
		else {
			String hql = "from VBackRoleSystemModel where roleid = "+ rolesysmodel.getRoleid() 
					+" and menuParentid = "+ rolesysmodel.getSysid() +" order by displayorder";;
			List<VBackRoleSystemModel> list = dao.select(hql);
			List<Object> newList = new ArrayList<Object>();
			List<Integer> models = new ArrayList<Integer>();
			for(int i = 0; i < list.size(); i++){
				VBackRoleSystemModel vSystemModel = (VBackRoleSystemModel)list.get(i);
				TBackRoleSystemModel tSystemModel = (TBackRoleSystemModel) 
						dao.findById(TBackRoleSystemModel.class, vSystemModel.getId());			
				tSystemModel.setIsedit(false);
				newList.add(tSystemModel);
				models.add(dao.UPDATE);
			}
			TBackRoleSystemModel tSystemModel1 = (TBackRoleSystemModel) 
					dao.findById(TBackRoleSystemModel.class, rolesysmodel.getId());
			tSystemModel1.setIsedit(false);
			newList.add(tSystemModel1);
			models.add(dao.UPDATE);
			return dao.executeBatch(newList, models);
		}
		
	}

}
